/**
 * 将后端地址Code字段数据转换成地址级联组件接收的数组格式
 * {countryCode: '00', provinceCode: '40', cityCode: '40100', districtCode: '4010010'}
 * 转成
 * ['00', '40', '40100', '4010010']
 * or
 * 海外：['01']
 * @param record
 * @param arrayFieldKey
 * @param setCodeKeyMap
 */
export function addressCodeMapToArray(
  record: any,
  arrayFieldKey: string = 'addressMap',
  setCodeKeyMap?: Record<string, any>,
) {
  const defaultCodeKeyMap: any = {
    countryCodeKey: 'countryCode',
    provinceCodeKey: 'provinceCode',
    cityCodeKey: 'cityCode',
    districtCodeKey: 'districtCode',
  };

  if (setCodeKeyMap) {
    for (const key in setCodeKeyMap) {
      if (Object.prototype.hasOwnProperty.call(defaultCodeKeyMap, key)) {
        defaultCodeKeyMap[key] = setCodeKeyMap[key];
      }
    }
  }
  // console.log('defaultCodeKeyMap', defaultCodeKeyMap);

  // 转成地址级联组件接收的数据格式
  if (record[defaultCodeKeyMap.countryCodeKey]) {
    if (record[defaultCodeKeyMap.countryCodeKey] === '00') {
      // 中国
      const arr = [record[defaultCodeKeyMap.countryCodeKey]];
      if (record[defaultCodeKeyMap.provinceCodeKey]) {
        arr.push(record[defaultCodeKeyMap.provinceCodeKey]);
      }
      if (record[defaultCodeKeyMap.cityCodeKey]) {
        arr.push(record[defaultCodeKeyMap.cityCodeKey]);
      }
      if (record[defaultCodeKeyMap.districtCodeKey]) {
        arr.push(record[defaultCodeKeyMap.districtCodeKey]);
      }
      record[arrayFieldKey] = arr;
    } else {
      // 海外国家
      record[arrayFieldKey] = [record[defaultCodeKeyMap.countryCodeKey]];
    }
  } else {
    record[arrayFieldKey] = [];
  }

  return record;
}
